package com.tangyee;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.tangyee.R;
import com.tangyee.adapter.FirstFragmentPagerAdapter;
import com.tangyee.home.activity.Login;
import com.tangyee.home.activity.db.UsersInfo;
import com.tangyee.utils.PopupMenuUtil;
import com.tangyee.utils.SessionHelper;
import com.tangyee.utils.SessionReceiver;
import com.tangyee.utils.UserInfoUtil;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener, SessionHelper {

    private static final String TAG = "MainActivity";
    //几个代表页面的常量
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    //UI Objects
    private RadioGroup rg_tab_bar;
    private RadioButton home;
    private RadioButton person;
    private ViewPager viewpager;
    private FirstFragmentPagerAdapter Adapter;

    private ImageView ivImg;
    private RelativeLayout rlClick;
    private Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Adapter = new FirstFragmentPagerAdapter(getSupportFragmentManager());
        bindViews();
        home.setChecked(true);
        needUpdate();
    }
    public void needUpdate(){
        Intent intent = getIntent();
//        Log.d(TAG,intent.getStringExtra("preActivity"));
        try {
            if (intent.getStringExtra("preActivity").equals("BasicActivity.class") ){
                Log.d(TAG, "MainActivity需要发起更新,用户名为:" + intent.getStringExtra(USERNAME) + "\t" +
                        "密码为：" + intent.getStringExtra(PASSWORD));
                UsersInfo usersInfo = UserInfoUtil.getLoadedUser();
                //android 8.0之后必须Intent(context,targerClass);
                intent = new Intent(this, SessionReceiver.class);
                intent.setAction(I_NEED_SESSIONID_BY_BROAD);
                intent.putExtra(USERNAME, usersInfo.getUsername());
                intent.putExtra(PASSWORD, usersInfo.getPassword());
                sendBroadcast(intent);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public void bindViews() {
        context = this;
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        home = (RadioButton) findViewById(R.id.home);
        person = (RadioButton) findViewById(R.id.person);
        rg_tab_bar.setOnCheckedChangeListener(this);
        //底部栏弹出快捷菜单
        ivImg = (ImageView) findViewById(R.id.iv_img);
        rlClick = (RelativeLayout) findViewById(R.id.rl_click);
        rlClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenuUtil.getInstance()._show(context, ivImg);
            }
        });
        viewpager = (ViewPager) findViewById(R.id.viewpage);
        viewpager.setAdapter(Adapter);
        viewpager.setCurrentItem(0);
        //初始页面为主页
        viewpager.addOnPageChangeListener(this);
    }

    //重写页面的切换处理方法
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //state的状态有三个，0表示什么都没做，1正在滑动，2滑动完毕
        if (state == 2) switch (viewpager.getCurrentItem()) {
            case PAGE_ONE:
                home.setChecked(true);
                break;
            case PAGE_TWO:
                person.setChecked(true);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.home:
                viewpager.setCurrentItem(PAGE_ONE);
                break;
            case R.id.person:
                viewpager.setCurrentItem(PAGE_TWO);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        // 当popupWindow 正在展示的时候 按下返回键 关闭popupWindow 否则关闭activity
        if (PopupMenuUtil.getInstance()._isShowing()) {
            PopupMenuUtil.getInstance()._rlClickAction();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void registerSessionReceiver() {

    }

    @Override
    public void unregisterSessionReceiver() {

    }
}
